package org.jeecg.demo.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.demo.wx.entity.EScheduled;
import org.jeecg.demo.wx.request.CreateEScheduledRequest;
import org.jeecg.demo.wx.service.IEScheduledService;
import org.jeecg.demo.wx.vo.ScheduleDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: e_scheduled
 * @Author: jeecg-boot
 * @Date: 2024-01-30
 * @Version: V1.0
 */
@Api(tags = "e_scheduled")
@RestController
@RequestMapping("/wx/eScheduled")
@Slf4j
public class EScheduledController extends JeecgController<EScheduled, IEScheduledService> {
    @Autowired
    private IEScheduledService eScheduledService;


    @GetMapping("/getEScheduledList")
    @ApiOperation(value = "日程列表查询", notes = "日程列表查询")
    public Result<List<EScheduled>> getEScheduledList(@RequestParam("bdate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date bdate,
                                                      @RequestParam("edate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date edate,
                                                      @RequestParam("status") String status) {

        List<EScheduled> list = eScheduledService.getEScheduledList(bdate,edate,status);
        return Result.ok(list);
    }

    /**
     * 分页列表查询
     *
     * @param eScheduled
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "e_scheduled-分页列表查询")
    @ApiOperation(value = "e_scheduled-分页列表查询", notes = "e_scheduled-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<EScheduled>> queryPageList(EScheduled eScheduled,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        QueryWrapper<EScheduled> queryWrapper = QueryGenerator.initQueryWrapper(eScheduled, req.getParameterMap());
        Page<EScheduled> page = new Page<EScheduled>(pageNo, pageSize);
        IPage<EScheduled> pageList = eScheduledService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

//    @AutoLog(value = "e_scheduled-添加")
    @ApiOperation(value = "日程添加", notes = "日程添加")
//    @RequiresPermissions("wx:e_scheduled:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody CreateEScheduledRequest request) {
          eScheduledService.add(request);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param eScheduled
     * @return
     */
    @AutoLog(value = "e_scheduled-编辑")
    @ApiOperation(value = "e_scheduled-编辑", notes = "e_scheduled-编辑")
    @RequiresPermissions("wx:e_scheduled:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody EScheduled eScheduled) {
        eScheduledService.updateById(eScheduled);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "e_scheduled-通过id删除")
    @ApiOperation(value = "e_scheduled-通过id删除", notes = "e_scheduled-通过id删除")
    @RequiresPermissions("wx:e_scheduled:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        eScheduledService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "e_scheduled-批量删除")
    @ApiOperation(value = "e_scheduled-批量删除", notes = "e_scheduled-批量删除")
    @RequiresPermissions("wx:e_scheduled:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.eScheduledService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    //@AutoLog(value = "e_scheduled-通过id查询")
    @ApiOperation(value = "查询日程详情", notes = "查询日程详情")
    @GetMapping(value = "/queryById")
    public Result<ScheduleDetailVo> queryById(@RequestParam(name = "id", required = true) String id) {
        ScheduleDetailVo scheduleDetailVo = eScheduledService.queryById(id);
        return Result.OK(scheduleDetailVo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param eScheduled
     */
    @RequiresPermissions("wx:e_scheduled:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EScheduled eScheduled) {
        return super.exportXls(request, eScheduled, EScheduled.class, "e_scheduled");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("wx:e_scheduled:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EScheduled.class);
    }

}
